package com.it.controller;

import com.alibaba.fastjson.JSONObject;
import com.it.common.Result;
import com.it.dao.SongMapper;
import com.it.domain.ListSong;
import com.it.domain.Song;
import com.it.service.ListSongService;
import com.it.common.CONSTS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ListSong")
public class ListSongController {

    @Autowired
    private ListSongService listSongService;

    @Autowired
    private SongMapper songMapper;

    @PostMapping("/AddListSong")
    public Object AddListSong(@RequestBody ListSong listsong){

        String SongName = listsong.getSongName().trim();
        String album = listsong.getAlbum().trim();
        System.out.println(SongName);
        System.out.println(album);
        System.out.println(listsong.toString());
        Song song = songMapper.SelectBySong(SongName,album,null);
        if(song == null){
            return Result.fail("405","歌曲不存在，请添加歌曲");
        }
        else{
            listsong.setSongId(song.getId());
            listSongService.AddListSong(listsong);
            return Result.success("200","添加成功");
        }
    }

    @DeleteMapping("/DeleteListSong")
    public Object DeleteListSong(@RequestParam("songId") Integer songId,@RequestParam("songListId") Integer songListId){
        JSONObject jsonObject = new JSONObject();

        boolean flag = listSongService.DeleteListSong(songId,songListId);

        if (flag) {
            jsonObject.put(CONSTS.CODE, 200);
            jsonObject.put(CONSTS.MSG, "删除成功");
        } else {
            jsonObject.put(CONSTS.CODE, 500);
            jsonObject.put(CONSTS.MSG, "删除失败");
        }
        return jsonObject;
    }

    @PutMapping("/UpdateListSong")
    public Object UpdateListSong(@RequestBody ListSong listsong){

        JSONObject jsonobject = new JSONObject();

        Integer songId = listsong.getSongId();
        Integer songListId = listsong.getSongListId();

        ListSong listSong = new ListSong();

        listSong.setSongId(songId);
        listSong.setSongListId(songListId);

        boolean flag = listSongService.UpdateListSong(listSong);
        if (flag) {
            jsonobject.put(CONSTS.CODE, 200);
            jsonobject.put(CONSTS.MSG, "更新成功");
        } else {
            jsonobject.put(CONSTS.CODE, 400);
            jsonobject.put(CONSTS.MSG, "更新失败");
        }
        return jsonobject;
    }

    @GetMapping("/SelectBySongListId/{songListId}")
    public Object SelectBySongListId(@PathVariable Integer songListId){
         return listSongService.SelectBySongListId(songListId);
    }

    @GetMapping("/GetSongTotal/{songListId}")
    public Integer GetSongTotal(@PathVariable Integer songListId){
        return listSongService.GetSongTotal(songListId);
    }

    @GetMapping("/SelectAll")
    public List<ListSong> SelectAll(){
        return listSongService.SelectAll();
    }
}
